import { Modal, Space } from 'ant-design-vue'
import { defineComponent, nextTick, ref } from 'vue'

import { cinemaApi, orderApi } from '@/apis'
import { type ProTableProps, type ProTableRef, ProTable } from '@/components'
import { TABLE_ITEM_RENDER, TABLE_RENDERER } from '@/constants'

export default defineComponent({
  name: 'CinemaList',
  setup() {
    const province = ref('')
    const city = ref('')
    const selected = ref([] as string[])
    const tableRef = ref({} as ProTableRef)
    const tableConfig = ref({
      fixedHeight: 400,
      options: {
        border: true,
        align: 'center',
        formConfig: {
          items: [
            {
              title: '影院名称',
              field: 'name',
              itemRender: {
                name: TABLE_ITEM_RENDER.AInput,
                props: {
                  placeholder: '请输入影院名称'
                }
              }
            },
            {
              itemRender: {
                name: TABLE_RENDERER.FormItemBtns
              }
            }
          ]
        },
        columns: [
          {
            type: 'checkbox',
            width: 50
          },
          {
            title: '影院id',
            field: 'cinemaId',
            minWidth: 100
          },
          {
            title: '影院名称',
            field: 'cinemaName',
            minWidth: 150
          },
          {
            title: '所在省市',
            minWidth: 150,
            slots: {
              default: 'provinceRow'
            }
          },
          {
            title: '详细地址',
            field: 'cinemaAddress',
            minWidth: 200
          },
          {
            title: '影院电话',
            field: 'cinemaPhone',
            minWidth: 200
          }
        ],
        checkboxConfig: {
          showHeader: false,
          visibleMethod: ({ row }) => {
            return selectStatus(row)
          },
          range: true
        },
        rowConfig: {
          keyField: 'id'
        },
        pagerConfig: {
          pageSize: 15
        },
        proxyConfig: {
          form: true,
          props: {
            result: 'dataList',
            total: 'page.totalCount'
          },
          ajax: {
            query: async ({ page, form }) => {
              const data = await cinemaApi.getCinemaList({
                pageParameter: {
                  pageSize: page.pageSize,
                  currentPage: page.currentPage
                },
                province: province.value,
                city: city.value,
                ...form
              })
              return data
            }
          }
        }
      }
    } as ProTableProps)

    function selectStatus(row) {
      const result = selected.value.indexOf(row.id) == -1
      return result
    }

    function handleCinemaSelect() {
      return tableRef.value.table.getCheckboxRecords()
    }

    function showModal(selectProvince, selectCity, selectedList) {
      province.value = selectProvince
      city.value = selectCity
      if (selectedList.length > 0) {
        selected.value = selectedList.map(val => {
          return val.id
        })
      } else {
        selected.value = []
      }
      nextTick(async () => {
        await tableRef.value.table.commitProxy('query')
      })
    }

    return {
      tableRef,
      tableConfig,
      showModal,
      handleCinemaSelect
    }
  },
  render() {
    return (
      <div>
        <ProTable
          ref="tableRef"
          v-slots={{
            provinceRow: ({ row }) => (
              <Space>
                <span>{`${row.province ? row.province : ''}${
                  row.city ? row.city : ''
                }`}</span>
              </Space>
            )
          }}
          {...this.tableConfig}
        />
      </div>
    )
  }
})
